--[[
	FONTE: 
		* https://github.com/BlockMen/hud
		* https://github.com/BlockMen/hud/blob/master/init.lua
]]--

lunogauges.huds.hp_player = {}
lunogauges.huds.gaugehp = {}

lunogauges.huds.add_hp = function(player)
	if player~= nil and player:is_player() then
		lunogauges.huds.gaugehp[player:get_player_name()] = {}
		
		lunogauges.huds.gaugehp[player:get_player_name()].bg = player:hud_add({
			hud_elem_type = "statbar",
			position = {x=0.5,y=1},
			size = {x=32, y=32}, --{x=24, y=24},
			text = "hud_heart_bg.png",
			number = 20,
			alignment = {x=-1,y=-1},
			offset = {x=-330, y=-97}, --{x=-215, y=-97},
		})

		lunogauges.huds.gaugehp[player:get_player_name()].fg = player:hud_add({
			hud_elem_type = "statbar",
			position = {x=0.5,y=1},
			size = {x=32, y=32}, --{x=24, y=24},
			text = "hud_heart_fg.png",
			number = player:get_hp(),
			alignment = {x=-1,y=-1},
			offset = {x=-330, y=-97}, --{x=-215, y=-97},
		})
		lunogauges.huds.hp_player[player:get_player_name()] = player:get_hp()
	end
end

lunogauges.huds.update_hp= function(player)
	if player~= nil and player:is_player() then
		local name = player:get_player_name()
		local hp = player:get_hp()
		if lunogauges.huds.hp_player[name]== nil or lunogauges.huds.hp_player[name] ~= hp   then
			lunogauges.huds.hp_player[name] = hp
			if lunogauges.huds.gaugehp[name]~=nil and lunogauges.huds.gaugehp[name].fg ~= nil then
				player:hud_change(lunogauges.huds.gaugehp[name].bg, "number", 20) --Por causa de bug do 0.4.10 tem q atualizar de vez em quando o background tb.
				player:hud_change(lunogauges.huds.gaugehp[name].fg, "number", hp)
			else
				lunogauges.huds.add_hp(player)
			end
		end
	end
end


